<template>
  <div class="show-main">
    <el-scrollbar style="height:100%">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <el-form :inline="true" :model="searchForm" ref="searchForm">
            <el-form-item prop="name">
              <el-input v-model="searchForm.name" clearable placeholder="请输入公司名称"></el-input>
            </el-form-item>
            <el-form-item prop="is_believe">
              <el-select v-model="searchForm.is_believe" clearable placeholder="请选择授信状态">
                <el-option label="非授信" value="0"></el-option>
                <el-option label="授信" value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click.stop="search(true)">搜索</el-button>
              <el-button type="success" @click.stop="search">刷新</el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-table :data="table" border>
          <el-table-column prop="id" label="ID"></el-table-column>
          <el-table-column prop="company_name" label="公司名称"></el-table-column>
          <el-table-column prop="driver_name" label="司机">
            <template slot-scope="scope">
              <span v-if="scope.row.driver_id == 0" style="color:#F56C6C">---</span>
              <span v-else>{{scope.row.driver_name}}-{{scope.row.role_name}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="is_believe" label="授信" width="70">
            <template slot-scope="scope">
              <span v-if="scope.row.is_believe === 0" style="color:#F56C6C">非授信</span>
              <span v-if="scope.row.is_believe === 1" style="color:#409EFF">授信</span>
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="创建时间" width="150">
            <template slot-scope="scope">
              {{scope.row.create_time|formatDate}}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="scope">
              <el-dropdown trigger="click" @command="handleCommand">
                <el-button type="primary">
                  操作菜单<i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item :command="{cmd:'believe', id:scope.row.id}">授信</el-dropdown-item>
                  <el-dropdown-item :command="{cmd:'editCenter', id:scope.row.id}">分拣中心</el-dropdown-item>
                  <el-dropdown-item v-if="scope.row.driver_id === 0" :command="{cmd:'bindDriver', id:scope.row.id}">绑定司机</el-dropdown-item>
                  <el-dropdown-item v-else :command="{cmd:'unbindDriver', row:scope.row}">解绑司机</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="searchForm.page"
            :page-sizes="[10,20,30,40]" :page-size="searchForm.limit" layout="total, sizes, prev, pager, next, jumper" :total="count">
          </el-pagination>
        </div>
      </el-card>
      <el-dialog title="授信公司" :visible.sync="believeVisible" :before-close="handleClose('believeForm')" width="400px">
        <el-form ref="believeForm" :model="believeForm" label-width="80px" :rules="rules">
          <el-form-item label="授信" prop="is_believe">
            <el-radio-group v-model="believeForm.is_believe">
              <el-radio :label="1" border>授信</el-radio>
              <el-radio :label="0" border>非授信</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('believeVisible', 'believeForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('believeVisible','believeForm')" size="small">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="绑定司机" :visible.sync="driverVisible" :before-close="handleClose('driverForm')" width="400px">
        <el-form ref="driverForm" :model="driverForm" label-width="80px" :rules="rules">
          <el-form-item label="司机" prop="driver_id">
            <el-select v-model="driverForm.driver_id" placeholder="请选择司机" clearable>
              <el-option v-for="(item) in driverList" :key="item.id" :label="item.name" :value="item.id">
                <span style="float: left">{{ item.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.role_name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('driverVisible', 'driverForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('driverVisible','driverForm')" size="small">添 加</el-button>
        </span>
      </el-dialog>
      <el-dialog title="分拣中心" :visible.sync="centerVisible" :before-close="handleClose('centerForm')" width="600px">
        <el-form ref="centerForm" :model="centerForm" label-width="80px" :rules="rules">
          <el-form-item label="分拣中心" prop="center_ids">
            <el-select v-model="centerForm.center_ids" placeholder="请选择分拣中心" multiple style="width:100%">
              <el-option v-for="(item) in centerList" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="close('centerVisible', 'centerForm')" size="small">取 消</el-button>
          <el-button type="primary" @click="send('centerVisible','centerForm')" size="small">编 辑</el-button>
        </span>
      </el-dialog>
    </el-scrollbar>
  </div>
</template>
<script>
import {
  getCompanylist,
  believeCompany,
  getCompanyDriver,
  bindCompanyDriver,
  unbindCompanyDriver,
  getCompanyCenter,
  editCompanyCenter
} from '@/network/api.js'

export default {
  name: 'company',
  data() {
    return {
      searchForm: {
        page: 1,
        limit: 10,
        name: '',
        is_believe: ''
      },
      table: [],
      count: 0,
      believeForm: {
        id: 0,
        is_believe: 1
      },
      believeVisible: false,
      // 司机表单
      driverForm: {
        id: 0,
        driver_id: ''
      },
      driverVisible: false,
      driverList: [], // 司机列表
      // 分拣中心表单
      centerForm: {
        id: 0,
        center_ids: []
      },
      centerVisible: false,
      centerList: [], // 分拣中心列表
      rules: {
        is_adopt: [{ required: true, message: '名称必须', trigger: 'blur' }],
        is_believe: [{ required: true, message: '名称必须', trigger: 'blur' }],
        center_ids: [{ type: 'array', required: true, message: '至少选择一个', trigger: 'change' }]
      }
    }
  },
  created() {
    this.search()
  },
  computed: {},
  methods: {
    // 每页条数变化
    handleSizeChange(val) {
      this.searchForm.limit = val
      this.search()
    },
    // 当前页变化
    handleCurrentChange(val) {
      this.searchForm.page = val
      this.search()
    },
    async search(pageInit = false) {
      try {
        if (pageInit === true) {
          this.searchForm.page = 1
        }
        const rtn = await getCompanylist(this.searchForm)
        this.table = rtn.list
        this.count = rtn.count
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 授信
    believe(id) {
      this.believeForm.id = id
      this.believeVisible = true
    },
    // 绑定司机
    async bindDriver(id) {
      try {
        this.driverForm.id = id
        const rtn = await getCompanyDriver({})
        this.driverVisible = true
        this.$nextTick(() => {
          this.driverList = rtn
        })
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 解绑司机
    async unbindDriver(row) {
      this.$confirm('您确定要解绑司机吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            await unbindCompanyDriver({ id: row.id, driver_id: row.driver_id })
            this.search()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(() => {
          this.$message.warning('您已取消操作')
        })
    },
    // 编辑分拣中心
    async editCenter(id) {
      try {
        this.centerForm.id = id
        const rtn = await getCompanyCenter({ id })
        this.centerVisible = true
        this.$nextTick(() => {
          this.centerList = rtn.centerInfo
          // rtn.info.center_ids = rtn.info.center_ids.split(',')
          // if (rtn.info.center_ids.length > 0) {
          //   rtn.info.center_ids.forEach((item, index, arr) => {
          //     arr[index] = parseInt(item)
          //   })
          // }
          this.centerForm = rtn.info
        })
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    handleCommand(command) {
      switch (command.cmd) {
        case 'believe':
          this.believe(command.id)
          break
        case 'bindDriver':
          this.bindDriver(command.id)
          break
        case 'unbindDriver':
          this.unbindDriver(command.row)
          break
        case 'editCenter':
          this.editCenter(command.id)
          break
      }
    },
    // 关闭弹出前操作(不包括取消按钮)
    handleClose(formName) {
      return done => {
        this.$refs[formName].clearValidate()
        this.$refs[formName].resetFields()
        done()
      }
    },
    // 取消按钮
    close(visibleName, formName) {
      this.$refs[formName].clearValidate()
      this.$refs[formName].resetFields()
      this[visibleName] = false
    },
    send(visibleName, formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          switch (formName) {
            case 'believeForm': // 授信
              try {
                await believeCompany(this.believeForm)
                this.close(visibleName, formName)
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
            case 'driverForm': // 司机
              try {
                await bindCompanyDriver(this.driverForm)
                this.close(visibleName, formName)
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
            case 'centerForm': // 分拣中心
              try {
                await editCompanyCenter(this.centerForm)
                this.close(visibleName, formName)
                this.search()
              } catch (e) {
                this.$message.error(e.message)
              }
              break
          }
        } else {
          this.$message.error('请检查表单')
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
